fix(otel): bridge Vercel AI SDK TTFT to completion_start_time#810
Open
ryanyue123 wants to merge 1 commit into
Open
fix(otel): bridge Vercel AI SDK TTFT to completion_start_time#810ryanyue123 wants to merge 1 commit into
ryanyue123 wants to merge 1 commit into
Conversation
`LangfuseSpanProcessor` did not convert the AI SDK's `ai.response.msToFirstChunk` (streamText) / `ai.stream.msToFirstChunk` (streamObject) attributes into `langfuse.observation.completion_start_time`, so generation observations land in Langfuse with TTFT blank. `langfuse-vercel` handled this in langfuse#379 and langfuse#401. Port the same arithmetic to `LangfuseSpanProcessor` — old exporter set a property on the ingestion event, new processor sets the attribute on the span before OTLP export. A user-set `completion_start_time` is preserved.
|
@ryanyue123 is attempting to deploy a commit to the langfuse Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LangfuseSpanProcessordoesn't setcompletionStartTimefor Vercel AI SDKspans, so TTFT is always blank in the UI.
The AI SDK emits
ai.response.msToFirstChunk(streamText) andai.stream.msToFirstChunk(streamObject). Nothing reads them.langfuse-vercelhandled this in #379 and #401. Lost in the move to@langfuse/otel.Fix
Port the #379/#401 bridge. Same math, same attribute names. Old exporter set
a property on the ingestion event; new processor sets the attribute on the
span before OTLP export.
A user-set
completion_start_timeis preserved.Verification
pnpm typecheck— greenpnpm lint— greenpnpm exec vitest run packages/otel/src/span-processor.test.ts— 19/19 pass (5 new)Greptile Summary
This PR ports the TTFT (time-to-first-token) bridge from the old
langfuse-vercelexporter intoLangfuseSpanProcessor, socompletionStartTimeis populated for Vercel AI SDK spans in the new OTEL pipeline.span-processor.ts: OnonEnd, if the span's instrumentation scope is"ai"andlangfuse.observation.completion_start_timeis not already set, it readsai.response.msToFirstChunk(streamText) orai.stream.msToFirstChunk(streamObject) and computesstartTime + msto derive the absolute first-chunk timestamp.streamTextpath, thestreamObjectpath, the no-op path when neither attribute is present, the scope guard (non-AI spans are skipped), and the pre-existing-value guard (user-set values are not overwritten).Confidence Score: 5/5
Safe to merge — the change is narrowly scoped to spans from the Vercel AI SDK instrumentation scope, never mutates spans that already carry a user-set value, and is guarded by a type check on the numeric attribute before writing anything.
The logic is a faithful port of the same computation that existed in the old exporter, the math is verified by the new tests, the guard conditions (scope name, pre-existing attribute, typeof ms === 'number') are all correct, and the attribute format matches what the rest of the codebase produces via _serialize. No imports were added inside functions.
No files require special attention.
Sequence Diagram
sequenceDiagram participant SDK as Vercel AI SDK participant OTel as OpenTelemetry participant Proc as LangfuseSpanProcessor.onEnd participant Export as OTLP Exporter SDK->>OTel: "end span (ai.response.msToFirstChunk = N or ai.stream.msToFirstChunk = N)" OTel->>Proc: onEnd(span) Proc->>Proc: shouldExportSpan? alt "scope == 'ai' AND completion_start_time not set" Proc->>Proc: "ms = msToFirstChunk attribute" Proc->>Proc: "completion_start_time = JSON.stringify(new Date(startTime + ms))" end Proc->>Proc: applyMaskInPlace(span) Proc->>Export: processor.onEnd(span) with completion_start_time set Export->>Export: OTLP export to LangfuseReviews (1): Last reviewed commit: "fix(otel): bridge Vercel AI SDK TTFT to ..." | Re-trigger Greptile